package StudyCode.summary.myfile;

import java.util.Arrays;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class ConvertByteArrayToIntArray {
	public static void main(String[] argv) {
		byte[] bytes = new byte[]{34, 35, 36, 37, 37, 37, 67, 68};
		System.out.println("字节数组" + Arrays.toString(bytes) + "转为 int 数组:");
		System.out.println(java.util.Arrays.toString(readInts(bytes)));
	}

	private static int[] readInts(byte[] bytes) {
		int[] ints = new int[bytes.length >> 2];
		for (int i = 0; i < ints.length; i++) {
			ints[i] = readInt(bytes, i << 2);
		}
		return ints;
	}


	private static int readInt(byte[] bytes, int offset) {
		return (bytes[offset] << 24) + ((bytes[offset + 1] & 0xff) << 16)
				+ ((bytes[offset + 2] & 0xff) << 8)
				+ (bytes[offset + 3] & 0xff);
	}
}
